#include "muduo/net/EventLoop.h"
#include "muduo/net/InetAddress.h"
#include "muduo/net/TcpServer.h"
#include <stdio.h>

using namespace muduo;
using namespace muduo::net;
//在连接时的回调函数
void onConnection(const TcpConnectionPtr &conn)
{
    if (conn->connected())
    {
        printf("onConnection():new connection [%s] from %s\n",
               conn->name().c_str(),
               conn->peerAddress().toIpPort().c_str());
    }
    else
    {
        printf("onConnection(): connection [%s] is down\n", conn->name().c_str());
    }
}
//收到客户端发送来的信息时的回调函数
void onMessage(const TcpConnectionPtr &conn, Buffer *data,Timestamp len)
{
    printf("onMessage():received %zd bytes from connection [%s]\n", data->readableBytes(), conn->name().c_str());
}

int main()
{
    printf("main():pid = %d\n",getpid());
    InetAddress listenAddr(9981);
    EventLoop loop;
    TcpServer server(&loop,listenAddr,"1",TcpServer::Option::kReusePort);
    server.setConnectionCallback(onConnection);
    server.setMessageCallback(onMessage);
    server.start();
    loop.loop();
    return 1;
}